package project02.controller.message;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import project02.dao.MessageDataDao;
import project02.model.MessageData;
import project02.model.UserInfo;

public class IndexController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(IndexController.class.getName());

    @Override
    public Navigation run() {

        UserInfo user = sessionScope("userInfo");

        List<MessageData> resultList = new ArrayList<MessageData>();
        resultList = getList(user.getUserId());

        if (resultList == null) {
            requestScope("result", "<script type=\"text/javascript\"> window.alert('表示する勤怠データが存在しません.'); </script>");
        }
        requestScope("resultList", resultList);

        return forward("index.jsp");
    }

    private List<MessageData> getList(String userId) {
        MessageDataDao dao = new MessageDataDao();
        List<MessageData> list = dao.findAllByUser(userId);
        // エンティティが1件もない
        if (list.size() == 0) {
            return null;
        }

        return list;
    }
}
